package com.itheima.stock.handler;

import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.StockException;
import com.itheima.stock.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.itheima.stock.handler
 * Description:
 *
 * @Author Lee
 * @Create 2024/10/30
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义业务异常
     *
     * @param e 异常信息
     * @return ResponseResult
     */
    @ExceptionHandler(value = StockException.class)
    public ResponseResult<?> handleException(StockException e) {
        return ResponseResult.error(e.getCode());
    }


    /**
     * 处理未知异常
     *
     * @param e 未知异常信息
     * @return ResponseResult
     */
    //压制
    @SuppressWarnings("all")
    @ExceptionHandler(value = Exception.class)
    public ResponseResult<?> handleUnKnownException(Exception e) {
        log.error("程序出异常了，异常信息:", e);
        //TODO
        e.printStackTrace();
        return ResponseResult.error(CodeEnum.SYSTEM_ERROR);
    }

}
